﻿// Author: Iulian Lita
// Project: ValidationModule
// Path: C:\Users\Tym\Documents\Visual Studio 2008\Projects\proiectIP\ValidationModule
// Creation date: 1/11/2009 10:52 PM
// Last modified: 1/12/2009 4:12 AM

#region Using directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
#endregion

namespace ValidationModule
{
    /// <summary>
    /// Field validator
    /// </summary>
    public class FieldValidator
    {
        /// <summary>
        /// Valid name
        /// numai litere, spatii, apostrof
        /// intre 1 si 40 caractere
        /// </summary>
        /// <param name="name">Name</param>
        public static bool ValidName(string name)
        {
            if (name == null)
                return false;
            if (Regex.IsMatch(name, @"^[a-zA-Z'\s]{1,40}$") == true)
                return true;
            return false;
        } // ValidName(name)

        /// <summary>
        /// Valid username
        /// </summary>
        /// <param name="username">Username</param>
        public static bool ValidUsername(string username)
        {
            if (username == null)
                return false;
            if (Regex.IsMatch(username, @"^[a-zA-Z0-9_]{5,20}$") == true)
                return true;
            return false;
        } // ValidUsername(username)

        /// <summary>
        /// Valid password
        /// numai litere si cifre
        /// intre 5 si 10 caractere
        /// </summary>
        /// <param name="password">Password</param>
        public static bool ValidPassword(string password)
        {
            if (password == null)
                return false;
            if (Regex.IsMatch(password, @"^[a-zA-Z\d]{5,10}$") == true)
                return true;
            return false;
        } // ValidPassword(password)

        /// <summary>
        /// Valid CNP
        /// </summary>
        /// <param name="cnp">Cnp</param>
        public static bool ValidCNP(string CNP)
        {
            if (CNP == null || CNP.Length != 13)
                return false;

            string Pondere = "279146358279";
            int WSUMA = 0, i = 1;

            for (i = 0; i < 12; i++)
            {
                WSUMA += (int.Parse(Pondere[i].ToString()) * int.Parse(CNP[i].ToString()));
            }
            WSUMA %= 11;

            if (WSUMA == 10)
                WSUMA = 1;

            if (WSUMA != int.Parse(CNP[12].ToString()))
                return false;

            return true;
        } // ValidCNP(CNP)

        /// <summary>
        /// Valid email
        /// </summary>
        /// <param name="email">Email</param>
        public static bool ValidEmail(string email)
        {
            if (email == null)
                return false;
            if (Regex.IsMatch(email, @"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$") == true)
                return true;
            return false;
        } // ValidEmail(email)

        /// <summary>
        /// Valid book title
        /// </summary>
        /// <param name="bookTitle">Book title</param>
        public static bool ValidBookTitle(string bookTitle)
        {
            return ValidName(bookTitle);
        } // ValidBookTitle(bookTitle)
    } // class FieldValidator
} // namespace ValidationModule
